package com.code.app.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.BaseInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.code.app.R;

import java.util.Random;

public class LoveLayout extends RelativeLayout {

    private Drawable drawables[];

    private Interpolator interpolators[];

    private LayoutParams layoutParams;

    private int drawableWidth, drawableHeight;

    private Random random;

    private int height, width;

    public LoveLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        random = new Random();

        drawables = new Drawable[3];
        drawables[0] = getResources().getDrawable(R.mipmap.ic_launcher);
        drawables[1] = getResources().getDrawable(R.mipmap.ic_launcher_round);
        drawables[2] = getResources().getDrawable(R.mipmap.ic_launcher_round);
        drawableHeight = drawables[0].getIntrinsicHeight();
        drawableWidth = drawables[0].getIntrinsicWidth();

        interpolators = new BaseInterpolator[4];
        interpolators[3] = new DecelerateInterpolator();// 减速
        interpolators[0] = new LinearInterpolator();// 线性
        interpolators[1] = new AccelerateDecelerateInterpolator();// 先加速后减速
        interpolators[2] = new AccelerateInterpolator();// 加速

        layoutParams = new LayoutParams(drawableWidth, drawableHeight);
        layoutParams.addRule(CENTER_HORIZONTAL);
        layoutParams.addRule(ALIGN_PARENT_BOTTOM, TRUE);

    }

    public void addLove() {
        final ImageView imageView = new ImageView(getContext());
        imageView.setImageDrawable(drawables[random.nextInt(drawables.length)]);
        imageView.setLayoutParams(layoutParams);
        addView(imageView);

        AnimatorSet animatorSet = getAnimatorSet(imageView);
        animatorSet.start();
    }

    private AnimatorSet getAnimatorSet(ImageView imageView) {
        ObjectAnimator alphe = new ObjectAnimator().ofFloat(imageView, "alpha", 0.3f, 0.5f);
        ObjectAnimator scalex = ObjectAnimator.ofFloat(imageView, "scaleX", 0.2f, 1f);
        ObjectAnimator scaley = ObjectAnimator.ofFloat(imageView, "scaleY", 0.2f, 1f);
        AnimatorSet enter = new AnimatorSet();
        enter.setDuration(500);
        enter.playTogether(alphe, scalex, scaley);
        enter.setTarget(imageView);
        return enter;
    }




    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
    }
}
